Skip to content

TupleTreeModel

Base classes

Name Children Inherits
TreeModel
prettyqt.itemmodels.treemodel
TreeModel which is used as a base for most TreeModels implemented.

⋔ Inheritance diagram

graph TD
  1946373679["itemmodels.TupleTreeModel"]
  2846913429["itemmodels.TreeModel"]
  874904086["core.AbstractItemModel"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2846913429 --> 1946373679
  874904086 --> 2846913429
  3882849576 --> 874904086
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  1898060022 --> 874904086
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: TreeModel

Base Tree Model to display a dict[tuple[Any], str] data structure.

The dictionary keys are tuples of path parts, like ("path", "to", "something")

mapping = {("a",): "test2", ("a", "b"): "test", ("a", "b", "c"): "test3"}
model = TupleTreeModel(mapping)
table = widgets.TreeView()
table.set_model(model)
table.show()
Source code in prettyqt/itemmodels/tupletreemodel.py
class TupleTreeModel(itemmodels.TreeModel):
    """Base Tree Model to display a dict[tuple[Any], str] data structure.

    The dictionary keys are tuples of path parts, like ("path", "to", "something")

    ```py
    mapping = {("a",): "test2", ("a", "b"): "test", ("a", "b", "c"): "test3"}
    model = TupleTreeModel(mapping)
    table = widgets.TreeView()
    table.set_model(model)
    table.show()
    ```
    """

    SUPPORTS = dict[tuple[Hashable, ...] | pathlib.PurePath, str]
    HEADER: ClassVar = ["Name"]

    def __init__(self, mapping: dict, **kwargs):
        self.mapping = {(): "root"} | {_to_tuple(k): v for k, v in mapping.items()}
        super().__init__((), **kwargs)

    def columnCount(self, parent: core.ModelIndex | None = None):
        return len(self.HEADER)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role, section:
            case constants.HORIZONTAL, constants.DISPLAY_ROLE, _:
                return self.HEADER[section]
        return None

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        tup = self.data_by_index(index).obj
        match role, index.column():
            case constants.DISPLAY_ROLE, 0:
                # intermediate nodes may not be part of the mapping.
                value = self.mapping.get(tup)
                return str(tup[-1] if value is None and tup else value)
            case constants.USER_ROLE, _:
                return tup
        return None

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case dict() if instance and all(
                isinstance(k, pathlib.PurePath | tuple) for k in instance
            ):
                return True
        return False

    def _child_keys(self, parts: tuple) -> list[tuple]:
        depth = len(parts)
        # dict keeps insertion order and removes duplicates
        children = {
            k[: depth + 1]: None
            for k in self.mapping
            if len(k) > depth and k[:depth] == parts
        }
        return list(children)

    def _fetch_object_children(
        self, item: TupleTreeModel.TreeItem
    ) -> list[TupleTreeModel.TreeItem]:
        return [self.TreeItem(obj=k) for k in self._child_keys(item.obj)]

    def _has_children(self, item: TupleTreeModel.TreeItem) -> bool:
        depth = len(item.obj)
        return any(len(k) > depth and k[:depth] == item.obj for k in self.mapping)

:fa-brands-github: Show source on GitHub